package cn.acmenms.controller;

import cn.acmenms.exception.TokenException;
import cn.acmenms.manager.authorization.TokenManager;
import cn.acmenms.po.enums.SourceFrom;
import cn.acmenms.po.vo.TokenUser;
import cn.acmenms.utils.Constants;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;

/**
 * @ClassName: BaseController
 * @Description: 公共Controller
 * @author: 神奇的鸭鸭
 * @Date: 2017/12/28 13:46
 */
public class BaseController {

    @Resource
    private TokenManager tokenManager;

    /**
     * 给classz类型的obj反射注入基本用户信息
     *
     * @param classz  要注入信息的类的类型
     * @param obj     要注入信息的对象
     * @param request 请求头
     */
    void setUserBaseInfo(Class<?> classz, Object obj, HttpServletRequest request) throws Exception {
        TokenUser tokenUser = this.getTokenUser(request);
        try {
            Integer userId = tokenUser.getUserId();
            String userName = tokenUser.getUserName();
            String userIcon = tokenUser.getUserIcon();
            Method userMethod = classz.getDeclaredMethod("setUserId", Integer.class);
            userMethod.invoke(obj, userId);
            Method userIconMethod = classz.getDeclaredMethod("setUserIcon", String.class);
            userIconMethod.invoke(obj, userIcon);
            Method userNameMethod = classz.getDeclaredMethod("setUserName", String.class);
            userNameMethod.invoke(obj, userName);
        } catch (Exception e) {
            throw new TokenException("无法通过TokenManager获取UserInfo:", e);
        }
    }

    /**
     * 通过Token获得TokenUser
     *
     * @param request 请求头
     * @return tokenUser
     */
    TokenUser getTokenUser(HttpServletRequest request) throws Exception {
        return tokenManager.checkToken(request.getHeader(Constants.DEFAULT_TOKEN_NAME));
    }

    /**
     * 获得IP: X-Forwarded-For:简称XFF头，它代表客户端，也就是HTTP的请求端真实的IP
     *
     * @param request 请求头
     * @return 客户端ip
     */
    String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    /**
     * 通过user-agent获得请求客户端类型
     *
     * @param request 请求头
     * @return 客户端类型
     */
    String getUserAgent(HttpServletRequest request) {
        String sourceFrom = request.getHeader("user-agent").toLowerCase();
        if (sourceFrom.contains(SourceFrom.PC.getDesc())) {
            return SourceFrom.PC.getDesc();
        } else if (sourceFrom.contains(SourceFrom.A.getDesc())) {
            return SourceFrom.A.getDesc();
        } else if (sourceFrom.contains(SourceFrom.I.getDesc())) {
            return SourceFrom.I.getDesc();
        }
        return "acmenms.cn";
    }
}